/*
 * Copyright 2008 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package blms.services.internal;

import java.io.File;
import java.io.Serializable;
import java.util.Collection;
import java.util.List;

import blms.domain.League;
import blms.domain.User;
import blms.exception.RestrictionException;
import blms.exception.UserDoesNotExistException;
import blms.exception.ValidationException;
import blms.repository.LeagueRepository;
import blms.repository.UserRepository;
import blms.services.UserService;
import blms.util.BLMSMessages;

/**
 * Default implementation of <code>UserService</code>
 * 
 * @author Jonhnny Weslley
 * @author Thiago Emmanuel Pereira da Cunha Silva, thiago.manel@gmail.com
 * @version 1.00, 21/04/2008
 * @since 1.0
 */
public class UserServiceImpl implements UserService {

	private final UserRepository users;
	private final LeagueRepository leagues;

	/**
	 * @param users
	 * @param leagues
	 * @param userValidator
	 */
	public UserServiceImpl(final UserRepository users,
			final LeagueRepository leagues) {
		this.users = users;
		this.leagues = leagues;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see blms.services.UserService#createUser(java.lang.String,
	 *      java.lang.String, java.lang.String, java.lang.String,
	 *      java.lang.String, java.lang.String, java.io.File)
	 */
	public User createUser(final String firstName, final String lastName,
			final String homePhone, final String workPhone,
			final String cellPhone, final String email, final File picture)
			throws RestrictionException, ValidationException {

		if (containsEmail(email)) {
			throw new RestrictionException(
					BLMSMessages.USER_WITH_THIS_MAIL_EXISTS_MESSAGE);
		}

		final User user = new User(firstName, lastName, homePhone, workPhone,
				cellPhone, email, picture);
		user.validate();
		users.add(user);
		return user;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see blms.services.UserService#getUser(java.lang.String)
	 */
	public User getUser(final Serializable id) throws UserDoesNotExistException {
		if (!contains(id)) {
			throw new UserDoesNotExistException(
					BLMSMessages.UNKNOWN_USER_MESSAGE);
		}
		return users.get(id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see blms.services.UserService#contains(java.io.Serializable)
	 */
	public boolean contains(final Serializable id) {
		return users.contains(id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see blms.services.UserService#containsEmail(java.lang.String)
	 */
	public boolean containsEmail(final String email) {
		return users.containsEmail(email);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see blms.services.UserService#findUserByEmail(java.lang.String)
	 */
	public User findUserByEmail(final String email)
			throws UserDoesNotExistException {
		return users.findUserByEmail(email);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see blms.services.UserService#findUserByLastName(java.lang.String)
	 */
	public List<String> findUserByLastName(final String lastName)
			throws UserDoesNotExistException {
		return users.findUserByLastName(lastName);
	}

	public void updateUser(final User user) throws ValidationException {
		user.validate();
		users.update(user);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see blms.services.UserService#deleteUser(java.lang.String)
	 */
	public void deleteUser(final Serializable id) throws UserDoesNotExistException, RestrictionException {

		final User user = getUser(id);
		final Collection<League> allLeagues = leagues.getAll();

		for (final League league : allLeagues) {
			if (league.isOperator(user)) {
				throw new RestrictionException(
						BLMSMessages.CANNOT_REMOVE_LEAGUE_OPERATOR_MESSAGE);
			}
		}

		users.remove(user);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see blms.services.UserService#removeAllUsers()
	 */
	public void removeAllUsers() {
		users.removeAll();
	}

}